<template>
  <div>
    <div class="divider-content-only divider-content-center-only">
      <a-divider type="vertical" class="divider-only"/>
      收入类别-其他收入，账单临时收入
    </div>
    <a-card :bordered="false">
      <div class="table-operator">
        <a-button type="primary" icon="plus" @click="$refs.addForm.add('1','1')">添加一級收入</a-button>
        <a-button type="primary" icon="plus" @click="$refs.addFormEj.add('1','2')">添加二級收入</a-button>
      </div>
      <a-table
        ref="table"
        size="middle"
        :rowKey="(record) => record.id"
        :pagination="false"
        :defaultExpandAllRows="true"
        :columns="columns"
        :dataSource="data"
        :loading="loading"
        showPagination="auto"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      >
        <span slot="level" slot-scope="text">
          {{ typeFilter(text) }}
        </span>
        <span slot="action" slot-scope="text, record">
          <a v-if="record.level === '1'" @click="$refs.editForm.edit(record,'1','1')">编辑</a>
          <a v-if="record.level === '2'" @click="$refs.editFormEj.edit(record,'1','2')">编辑</a>
          <a-divider type="vertical"/>
          <a-popconfirm placement="topRight" title="确认删除？" @confirm="() => busCostIncomeExpensesDelete(record)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </a-table>
      <add-form ref="addForm" @ok="handleOk"/>
      <edit-form ref="editForm" @ok="handleOk"/>
      <add-form-ej ref="addFormEj" @ok="handleOk"/>
      <edit-form-ej ref="editFormEj" @ok="handleOk"/>
    </a-card>
    <div class="divider-content-only divider-content-center-only">
      <a-divider type="vertical" class="divider-only"/>
      支出类别-其他支出，账单临时支出
    </div>
    <a-card :bordered="false">
      <div class="table-operator">
        <a-button type="primary" icon="plus" @click="$refs.addForm.add('2','1')">添加一級支出</a-button>
        <a-button type="primary" icon="plus" @click="$refs.addFormEj.add('2','2')">添加二級支出</a-button>
      </div>
      <a-table
        ref="table"
        size="middle"
        :rowKey="(record) => record.id"
        :pagination="false"
        :defaultExpandAllRows="true"
        :columns="columns"
        :dataSource="data2"
        :loading="loading"
        showPagination="auto"
        :row-selection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
      >
        <span slot="level" slot-scope="text">
          {{ typeFilter(text) }}
        </span>
        <span slot="action" slot-scope="text, record">
          <a v-if="record.level === '1'" @click="$refs.editForm.edit(record)">编辑</a>
          <a v-if="record.level === '2'" @click="$refs.editFormEj.edit(record)">编辑</a>
          <a-divider type="vertical"/>
          <a-popconfirm placement="topRight" title="确认删除？" @confirm="() => busCostIncomeExpensesDelete(record)">
            <a>删除</a>
          </a-popconfirm>
        </span>
      </a-table>
      <add-form ref="addForm" @ok="handleOk"/>
      <edit-form ref="editForm" @ok="handleOk"/>
      <add-form-ej ref="addFormEj" @ok="handleOk"/>
      <edit-form-ej ref="editFormEj" @ok="handleOk"/>
    </a-card>
  </div>
</template>
<script>
  import { STable } from '@/components'
  import { busCostIncomeExpensesList, busCostIncomeExpensesDelete } from '@/api/modular/main/buscostincomeexpenses/busCostIncomeExpensesManage'
  import addForm from './addForm.vue'
  import addFormEj from './addFormEj.vue'
  import editForm from './editForm.vue'
  import EditFormEj from './editFormEj.vue'
  export default {
    components: {
      STable,
      addForm,
      editForm,
      addFormEj,
      EditFormEj
    },
    data () {
      return {
        data: [],
        data2: [],
        loading: true,
        // 查询参数
        queryParam: {
          type: 1
        },
        // 表头
        columns: [
          {
            title: '名称',
            align: 'left',
            dataIndex: 'name'
          },
          {
            title: '级别',
            align: 'left',
            dataIndex: 'level',
            scopedSlots: { customRender: 'level' }
          }
        ],
        tstyle: { 'padding-bottom': '0px', 'margin-bottom': '10px' },
        selectedRowKeys: [],
        selectedRows: [],
        levelDict: [ { 'value': '一級類別', 'code': '1' }, { 'value': '二級類別', 'code': '2' } ]
      }
    },
    created () {
      this.loadData()
      this.columns.push({
          title: '操作',
          width: '150px',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
      })
    },
    methods: {
      typeFilter (type) {
        const values = this.levelDict.filter(item => item.code === type)
        if (values.length > 0) {
          return values[0].value
        }
      },
      loadData () {
        this.loading = true
        busCostIncomeExpensesList({ type: 1 }).then((res) => {
          if (res.code === 200) {
            this.data = res.data
          }
        }).finally(() => {
          this.loading = false
        })
        busCostIncomeExpensesList({ type: 2 }).then((res) => {
          if (res.code === 200) {
            this.data2 = res.data
          }
        }).finally(() => {
          this.loading = false
        })
      },
      busCostIncomeExpensesDelete (record) {
        busCostIncomeExpensesDelete(record).then((res) => {
          if (res.code === 200) {
            this.loadData()
            this.$message.success('删除成功')
          } else {
            this.$message.error(res.msg) // + res.message
          }
        })
      },
      handleOk () {
        this.loadData()
      },
      onSelectChange (selectedRowKeys, selectedRows) {
        this.selectedRowKeys = selectedRowKeys
        this.selectedRows = selectedRows
      }
    }
  }
</script>
<style lang="less">
  .table-operator {
    margin-bottom: 18px;
  }
  button {
    margin-right: 8px;
  }
  .divider-only{
    background-color: aqua;
  }
  .divider-content-only{
    font-size: 20px;
    margin-bottom: 10px;
  }
  .divider-content-center-only{
      margin-top: 10px;
  }
</style>
